package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 私聊列表功能实现类
 */
@Service
public class WeChatListServiceImpl extends ServiceImpl<WeChatListMapper, WeChatList> implements WeChatListService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的WeChatList表mapper对象
     */
    @Autowired
    private WeChatListMapper _WeChatListMpper;

 			
			
 			
			

    /**
     * 私聊列表分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<WeChatListDto> List(WeChatListPagedInput input) {

      
        //声明一个支持私聊列表查询的(拉姆达)表达式
        LambdaQueryWrapper<WeChatList> queryWrapper = Wrappers.<WeChatList>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,WeChatList::getId,input.getId())
                .eq(input.getCreatorId()!=null,WeChatList::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入BelongUserId不为空,则进行精确查询
        if(input.getBelongUserId()!=null) {
            queryWrapper=queryWrapper.eq(WeChatList::getBelongUserId,input.getBelongUserId());
       	 }
     	 //如果前端搜索传入ReceiveUserId不为空,则进行精确查询
        if(input.getReceiveUserId()!=null) {
            queryWrapper=queryWrapper.eq(WeChatList::getReceiveUserId,input.getReceiveUserId());
       	 }
     	 //如果前端搜索传入LastMessage不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getLastMessage())) {
            queryWrapper=queryWrapper.eq(WeChatList::getLastMessage,input.getLastMessage());
       	 }
     	 //如果前端传入了LastTime时间范围,则进行时间范围搜索
        if(input.getLastTimeRange()!=null&&input.getLastTimeRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(WeChatList::getLastTime,input.getLastTimeRange().get(1));
            queryWrapper=queryWrapper.gt(WeChatList::getLastTime,input.getLastTimeRange().get(0));
        }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(WeChatList::getCreationTime);
        //构建一个分页查询的model
        Page<WeChatList> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取私聊列表数据
        IPage<WeChatList> pageRecords= _WeChatListMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _WeChatListMpper.selectCount(queryWrapper);

        //把WeChatList实体转换成WeChatList传输模型
        List<WeChatListDto> items= Extension.copyBeanList(pageRecords.getRecords(),WeChatListDto.class);
        for (WeChatListDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto ReceiveUserDTO = new AppUserDto();        
            AppUser  ReceiveUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getReceiveUserId()));
           if(ReceiveUserEntity!=null) {  
          BeanUtils.copyProperties(ReceiveUserDTO,ReceiveUserEntity);
            item.setReceiveUserDto(ReceiveUserDTO); 
           }
          
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto BelongUserDTO = new AppUserDto();        
            AppUser  BelongUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getBelongUserId()));
           if(BelongUserEntity!=null) {  
          BeanUtils.copyProperties(BelongUserDTO,BelongUserEntity);
            item.setBelongUserDto(BelongUserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个私聊列表查询
     */
    @SneakyThrows
    @Override
    public WeChatListDto Get(WeChatListPagedInput input) {
       if(input.getId()==null)
        {
            return new WeChatListDto();
        }
        PagedResult<WeChatListDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new WeChatListDto();
    }

    /**
     *私聊列表创建或者修改
     */
    @SneakyThrows
    @Override
    public WeChatListDto CreateOrEdit(WeChatListDto input) {

        //声明一个私聊列表实体
        WeChatList WeChatList = new WeChatList();

        //把前端传入的input参数拷贝到私聊列表实体
        BeanUtils.copyProperties(WeChatList,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(WeChatList);

        //定义一个返回给前端的私聊列表传输模型
        WeChatListDto WeChatListDto=new WeChatListDto();

        //同理把操作的私聊列表实体拷贝给私聊列表传输模型
        BeanUtils.copyProperties(WeChatListDto,WeChatList);

        //把传输模型返回给前端
        return WeChatListDto;
    }



}
